//
//  LSFiltrateSubViewController.m
//  LookScore
//
//  Created by wws on 2018/4/24.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSFiltrateSubViewController.h"
//#import "LSCompetitionModel.h"
#import "LSFiltrateCompetitionCell.h"
#import "LSDataLeagueModel.h"
@interface LSFiltrateSubViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, assign) BOOL allBtnSelect;
//@property (nonatomic, strong) NSMutableArray * competitionArray;

@property (nonatomic, strong) NSMutableArray * zouLeagueArray;
@property (nonatomic, strong) NSMutableArray * comLeagueArray;
@property (strong,nonatomic)  UICollectionView *collectionView;
@property (nonatomic, assign) NSInteger type;//全部/滚球
@property (nonatomic, strong) UIButton *tmpButton;
@property (nonatomic, strong) UIButton * zouTmpButton;
@end

@implementation LSFiltrateSubViewController{
    UIButton * allBtn;
    UIButton * concentrationBtn;
    UILabel * _label;
}
//- (NSMutableArray *)competitionArray{
//    if (!_comLeagueArray) {
//        _comLeagueArray = @[].mutableCopy;
//    }
//
//    return _comLeagueArray;
//}

- (NSMutableArray *)zouLeagueArray{
    if (!_zouLeagueArray) {
        _zouLeagueArray = @[].mutableCopy;
    }
    return _zouLeagueArray;
}
- (NSMutableArray *)comLeagueArray{
    if (!_comLeagueArray) {
        _comLeagueArray = @[].mutableCopy;
    }
    return _comLeagueArray;
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing=0;
        flowLayout.minimumInteritemSpacing=0;
        flowLayout.itemSize=CGSizeMake(LSScreenWidth/4,36);
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0,0, LSScreenWidth,LSScreenHeight -64) collectionViewLayout:flowLayout];
        //        _collectionView.userInteractionEnabled=YES;
//        flowLayout.headerReferenceSize = CGSizeMake(LSScreenWidth, 50);
//        flowLayout.footerReferenceSize = CGSizeMake(LSScreenWidth, 50);
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
    }
    return _collectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = LSTitleWhiteColor;
    
    self.collectionView.frame = CGRectMake(0, 0, LSScreenWidth, LSScreenHeight - LSNavBarHeight - 35 -80);
    [self.collectionView registerClass:[LSFiltrateCompetitionCell class] forCellWithReuseIdentifier:NSStringFromClass([LSFiltrateCompetitionCell class])];
//    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
//    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [self.view addSubview:self.collectionView];
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
        
    }
    
    
    [self createView];
}

- (void)createView{
    UIView * bgView = [[UIView alloc] init];
    [self.view addSubview:bgView];
    bgView.sd_layout
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .topSpaceToView(self.collectionView, 0)
    .heightIs(80);
    
    UILabel * label = [[UILabel alloc] init];
    label.font = LSFontSize12;
    label.textColor = LSTitleGrayColor2;
    [bgView addSubview:label];
    _label = label;
    label.sd_layout
    .leftSpaceToView(bgView, 5)
    .topSpaceToView(bgView, 10)
    .autoHeightRatio(0);
    [label setSingleLineAutoResizeWithMaxWidth:180];
    
    allBtn = [[UIButton alloc] init];
    
    allBtn.titleLabel.font=LSFontSize13;
    allBtn.layer.cornerRadius=3;
    allBtn.layer.masksToBounds=YES;
    allBtn.layer.borderColor = LSTitleGrayColor3.CGColor;
    allBtn.layer.borderWidth=.5f;
    allBtn.backgroundColor = [UIColor whiteColor];
    [allBtn setTitle:@"全选" forState:UIControlStateNormal];
    [allBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [allBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [allBtn addTarget:self action:@selector(allSelectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:allBtn];
    
    allBtn.sd_layout
    .rightSpaceToView(bgView, 10)
    .centerYEqualToView(label)
    .heightIs(25)
    .widthIs(60);
    
    UIButton * afterBtn = [[UIButton alloc] init];
    
    afterBtn.titleLabel.font=LSFontSize13;
    afterBtn.layer.cornerRadius=3;
    afterBtn.layer.masksToBounds=YES;
    afterBtn.layer.borderColor = LSTitleGrayColor3.CGColor;
    afterBtn.layer.borderWidth=.5f;
    [afterBtn setTitle:@"反选" forState:UIControlStateNormal];
    afterBtn.backgroundColor = [UIColor whiteColor];
    
    [afterBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [afterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [afterBtn addTarget:self action:@selector(afterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:afterBtn];
    
    afterBtn.sd_layout
    .rightSpaceToView(allBtn, 10)
    .centerYEqualToView(label)
    .heightIs(25)
    .widthIs(60);
    
    
    
    concentrationBtn = [[UIButton alloc] init];
    
    concentrationBtn.titleLabel.font=LSFontSize13;
    concentrationBtn.layer.cornerRadius=3;
    concentrationBtn.layer.masksToBounds=YES;
    concentrationBtn.layer.borderColor = LSTitleGrayColor3.CGColor;
    concentrationBtn.layer.borderWidth=.5f;
    [concentrationBtn setTitle:@"精选" forState:UIControlStateNormal];
    concentrationBtn.backgroundColor = [UIColor whiteColor];
    
    [concentrationBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [concentrationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [concentrationBtn addTarget:self action:@selector(concentrationBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:concentrationBtn];
    
    concentrationBtn.sd_layout
    .rightSpaceToView(afterBtn, 10)
    .centerYEqualToView(label)
    .heightIs(25)
    .widthIs(60);
    
    
    
    UIButton * sureBtn = [[UIButton alloc] init];
    sureBtn.layer.cornerRadius = 5;
    [sureBtn addTarget:self action:@selector(sureBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:LSButtonColor];
    [sureBtn setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = LSFontSize12;
    [bgView addSubview:sureBtn];
    sureBtn.sd_layout
    .leftSpaceToView(bgView, 10)
    .rightSpaceToView(bgView, 10)
    .heightIs(30)
    .topSpaceToView(label, 15);
    
}

- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    if (self.type == 0) {
        concentrationBtn.selected = _allBtnSelect;
        if (_allBtnSelect) {
            concentrationBtn.backgroundColor = LSSubColorFF9E44;
            _tmpButton = concentrationBtn;
        }
    }else{
        
        concentrationBtn.selected = _allBtnSelect;
        if (_allBtnSelect) {
            concentrationBtn.backgroundColor = LSSubColorFF9E44;
            _zouTmpButton = concentrationBtn;
        }
    }
    
    if (self.type == 0) {
        _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.allDataArray.count - self.comLeagueArray.count];
    }else{
        
        _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.gunqiuDataArray.count - self.zouLeagueArray.count];
    }
}

- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    self.type = index;
    
    if (index == 0) {
    
        [self selectBtnWith:self.allDataArray];
       
    }else{
        for (NSInteger i = self.gunqiuDataArray.count-1; i>=0; i--) {
            LSDataLeagueModel * model = self.gunqiuDataArray[i];
            
            if (model.IsZouLeague) {
//               MyLog(@"1");
            }else{
//                MyLog(@"0");
                
                [self.gunqiuDataArray removeObjectAtIndex:i];
                
                
            }
        }
        [self selectBtnWith:self.gunqiuDataArray];
        
    }
   
    [self.collectionView reloadData];
    
}

- (void)selectBtnWith:(NSMutableArray *)array{
    //判断数组里面的模型的isSelected 是否全是yes 全为yes sectionHeader上的按钮为选中状态.
    for (LSDataLeagueModel * model in array) {
        if (model.IsTopClassLeague == YES) {
            if (model.isBtnSelected == YES) {
                _allBtnSelect = YES;
            }else{
                
                _allBtnSelect = NO;
                break;
            }
        }else{
            if (model.isBtnSelected == YES) {
                _allBtnSelect = NO;
                break;
            }
            
        }
    }
    //判断数组里面的模型isSelected是否为yes  是yes加到名字数组中
    for (LSDataLeagueModel * model in array) {
        if (model.isBtnSelected) {
            if (self.type == 0) {
                [self.comLeagueArray addObject:model.LeagueName_CN];
            }else{
                [self.zouLeagueArray addObject:model.LeagueName_CN];
            }
//            [self.competitionArray addObject:model.LeagueName_CN];
        }else{
            
            
        }
    }
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    if (self.type == 0) {
        
        return self.allDataArray.count;
    }
    return self.gunqiuDataArray.count;
   
}

/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LSFiltrateCompetitionCell * cell = [LSFiltrateCompetitionCell FiltrateCompetitionCellBycollectionView:collectionView indexPath:indexPath];
    if (self.type == 0) {
       cell.comModel = self.allDataArray[indexPath.row];
    }else{
        cell.comModel = self.gunqiuDataArray[indexPath.row];
        
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LSFiltrateCompetitionCell * cell =  (LSFiltrateCompetitionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    cell.competitionBu.selected=!cell.competitionBu.selected;
    LSDataLeagueModel * model;
    if (self.type == 0) {
        model = self.allDataArray[indexPath.row];
    }else{
        model = self.gunqiuDataArray[indexPath.row];
        
    }
    if (cell.competitionBu.selected) {
        model.isBtnSelected = YES;
        cell.competitionBu.backgroundColor = LSSubColorFF9E44;
        cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
        if (self.type == 0) {
            [self.comLeagueArray addObject:model.LeagueName_CN];
        }else{
            [self.zouLeagueArray addObject:model.LeagueName_CN];
        }
//        [self.competitionArray addObject: model.LeagueName_CN];
    }else{
        model.isBtnSelected = NO;
        if (self.type == 0) {
            [self.comLeagueArray removeObject:model.LeagueName_CN];
        }else{
            [self.zouLeagueArray removeObject:model.LeagueName_CN];
        }
//        [self.competitionArray removeObject:model.LeagueName_CN];
        cell.competitionBu.backgroundColor = [UIColor whiteColor];
        cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
    }
//    MyLog(@"%@",self.competitionArray);
    
}

- (void)sureBtnClicked:(UIButton *)sender{
    if (self.block) {
        if (self.type == 0){
           self.block(self.comLeagueArray, self.comLeagueArray, self.type +1);
        }else{
            
            self.block(self.zouLeagueArray, self.zouLeagueArray, self.type +1);
        }
        
    }
   
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)allSelectBtnAction:(UIButton *)sender{
 
    
    if (self.type == 0) {
        [self.comLeagueArray removeAllObjects];
        _tmpButton.backgroundColor = [UIColor whiteColor];
        _tmpButton.selected = sender.selected;
        _tmpButton = sender;
        sender.selected = YES;
        sender.backgroundColor = LSSubColorFF9E44;
        [self.allDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            cell.competitionBu.selected=!cell.competitionBu.selected;
            if (sender.selected) {
                sender.backgroundColor = LSSubColorFF9E44;
                
                model.isBtnSelected = YES;
                [self.comLeagueArray addObject: model.LeagueName_CN];
                cell.competitionBu.backgroundColor = LSSubColorFF9E44;
                cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
            }else{
                sender.backgroundColor = [UIColor whiteColor];
                model.isBtnSelected = NO;
                [self.comLeagueArray removeObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            }
        }];
        _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.allDataArray.count - self.comLeagueArray.count];
    }else{
        [self.zouLeagueArray removeAllObjects];
        _zouTmpButton.backgroundColor = [UIColor whiteColor];
        _zouTmpButton.selected = sender.selected;
        _zouTmpButton = sender;
        sender.selected = YES;
        sender.backgroundColor = LSSubColorFF9E44;
        [self.gunqiuDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            cell.competitionBu.selected=!cell.competitionBu.selected;
            if (sender.selected) {
                
                model.isBtnSelected = YES;
                [self.zouLeagueArray addObject: model.LeagueName_CN];
                cell.competitionBu.backgroundColor = LSSubColorFF9E44;
                cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
            }else{
                
                model.isBtnSelected = NO;
                [self.zouLeagueArray removeObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            }
        }];
       _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.gunqiuDataArray.count - self.zouLeagueArray.count];
    }
    [self.collectionView reloadData];
    
}

- (void)afterBtnClicked:(UIButton *)sender{

    if (self.type == 0) {
        [self.comLeagueArray removeAllObjects];
        _tmpButton.backgroundColor = [UIColor whiteColor];
        _tmpButton.selected = sender.selected;
        _tmpButton = sender;
        sender.selected = YES;
        sender.backgroundColor = LSSubColorFF9E44;
        [self.allDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            cell.competitionBu.selected=!cell.competitionBu.selected;
//            if (model.isBtnSelected) {
                model.isBtnSelected = NO;
                [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
                [self.comLeagueArray removeObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
//            }else{
//                model.isBtnSelected = YES;
//                NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
//                [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
//                [self.competitionArray addObject: model.LeagueName_CN];
//                cell.competitionBu.backgroundColor = LSSubColorFF9E44;
//                cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
//
//
//            }
        }];
        _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.allDataArray.count - self.comLeagueArray.count];
    }else{
        [self.zouLeagueArray removeAllObjects];
        _zouTmpButton.backgroundColor = [UIColor whiteColor];
        _zouTmpButton.selected = sender.selected;
        _zouTmpButton = sender;
        sender.selected = YES;
        sender.backgroundColor = LSSubColorFF9E44;
        [self.gunqiuDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            cell.competitionBu.selected=!cell.competitionBu.selected;
            model.isBtnSelected = NO;
            [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
            [self.zouLeagueArray removeObject:model.LeagueName_CN];
            cell.competitionBu.backgroundColor = [UIColor whiteColor];
            cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
        }];
        _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.gunqiuDataArray.count - self.zouLeagueArray.count];
    }
    
    [self.collectionView reloadData];
}

- (void)concentrationBtnClicked:(UIButton *)sender{
    
    if (self.type == 0) {
        [self.comLeagueArray removeAllObjects];
        _tmpButton.backgroundColor = [UIColor whiteColor];
        _tmpButton.selected = sender.selected;
        _tmpButton = sender;
        sender.selected = YES;
        sender.backgroundColor = LSSubColorFF9E44;
        [self.allDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            if (model.IsTopClassLeague) {
                [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
                cell.competitionBu.selected=!cell.competitionBu.selected;
                //            if (model.isBtnSelected) {
                model.isBtnSelected = YES;
                
                [self.comLeagueArray addObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            }else{
                
                model.isBtnSelected = NO;
                [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
                [self.comLeagueArray removeObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            }
            
            //            }else{
            //                model.isBtnSelected = YES;
            //                NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            //                [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
            //                [self.competitionArray addObject: model.LeagueName_CN];
            //                cell.competitionBu.backgroundColor = LSSubColorFF9E44;
            //                cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
            //
            //
            //            }
        }];
        _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.allDataArray.count - self.comLeagueArray.count];
    }else{
        [self.zouLeagueArray removeAllObjects];
        _zouTmpButton.backgroundColor = [UIColor whiteColor];
        _zouTmpButton.selected = sender.selected;
        _zouTmpButton = sender;
        sender.selected = YES;
        sender.backgroundColor = LSSubColorFF9E44;
        [self.gunqiuDataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            if (model.IsTopClassLeague) {
                [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
                cell.competitionBu.selected=!cell.competitionBu.selected;
                model.isBtnSelected = YES;
                [self.zouLeagueArray addObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
                
            }else{
                model.isBtnSelected = NO;
                [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
                [self.zouLeagueArray removeObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            }
        }];
        _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.gunqiuDataArray.count - self.zouLeagueArray.count];
    }
    [self.collectionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
